﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Alma.Models;
using AlmaDatabase;
using System.Diagnostics;

namespace Alma.Controllers
{
    public class CategoryController : Controller
    {
        public ActionResult Index()
        {
            using (var context = new AlmaDBEntities())
            {
                CategoriesModelList sList = new CategoriesModelList();
                sList.categoriesList = new List<CategoryModel>();
                var sections = context.Sections;
                using (var entities = new AlmaDBEntities())
                {
                    foreach (var category in (from p in entities.Categories select p))
                    {
                        CategoryModel sModel = new CategoryModel(category);
                        var realm = from p in entities.Realms where sModel.ID_Realm == p.ID select p;
                        sModel.Name_Realm = realm.First().Name;
                        sList.categoriesList.Add(sModel);
                    }
                }
                return View(sList.categoriesList);
            }
        }

        public ActionResult Show(int id)
        {
            SectionListModel sectionModel = new SectionListModel();
            sectionModel.List = new List<SectionDetailsModel>();
            using (var entities = new AlmaDBEntities())
            {
                foreach (var section in (from p in entities.Sections where p.ID_Category == id select p))
                {
                    SectionDetailsModel newSection = new SectionDetailsModel();
                    newSection.Name = section.Name;
                    newSection.ID = section.ID;
                    /**/
                    newSection.productList = new List<ProductDetailsModel>();
                    var prods = from products in entities.Products
                                where products.ID_Section == section.ID 
                                select products;
                    prods.Reverse();
                    int i = 0;
                    foreach (var p in prods)
                    {
                        ProductDetailsModel pMod = new ProductDetailsModel();
                        pMod.Name = p.Name;
                        pMod.ID = p.ID;
                        pMod.ImgUrl = p.ImgUrl;
                        if (pMod.ImgUrl == null)
                            pMod.ImgUrl = "~/Content/images/unavailable.gif";
                        newSection.productList.Add(pMod);
                        i++;
                        if (i == 4)
                            break;
                    }
                    /**/
                    sectionModel.List.Add(newSection);
                }
                var cat = (from p in entities.Categories where p.ID == id select p).First();
                sectionModel.Name = cat.Name;
                sectionModel.ID = id;
            }
            return View(sectionModel);
        }
    }
}
